<template>
	<view class="content">
		<luyj-choose-img @imgChange="imgChange"></luyj-choose-img>

		<view class="button-container">
			<button type="primary" class="button" @click="save">保存</button>

		</view>

	</view>
</template>

<script>
	import config from '@/config'
	import {
		getAccessToken,
		getTenantId
	} from '@/utils/auth'
	export default {
		data() {
			return {
				imagelist: [],
				urllist:[]

			}
		},
		methods: {
			imgChange(data) {
				console.log('data>>', data)
				this.imagelist = data
			},
			goback() {
			
				// let pages = getCurrentPages()
				// console.log('pages>>',pages)
				// let prevPage = pages[pages.length - 1]
				// prevPage.$vm.getImageList(this.imagelist)
				uni.$emit('imgUrls',{urllist:this.urllist})
				
				
				setTimeout(()=>{
					uni.hideLoading()
					uni.navigateBack({
						delta: 1
					})
				},1000)
				
			},
			uploadFile(){
				
				
			
				
			},
			save() {
				if (this.imagelist && this.imagelist.length > 0) {

					let urllist = []


					const isToken = (config.headers || {}).isToken === false
					config.header = config.header || {}
					if (getAccessToken() && !isToken) {
						config.header['Authorization'] = 'Bearer ' + getAccessToken()
					}
					// 设置租户
					config.header['tenant-id'] = getTenantId();
					try {
						uni.showLoading({
							title: '保存图片中...'
						})
						
						
						let promises = [];
					
						for (var i = 0; i < this.imagelist.length; i++) {
						 promises.push(new Promise((resolve, reject) => {
						        uni.uploadFile({
						            url: 'http://192.168.1.184:2928/admin-api/infra/file/upload', //仅为示例，非真实的接口地址
						            filePath: this.imagelist[i],
						            header: config.header,
						            name: 'file',
						            success: function(uploadFileRes) {
						                const data = uploadFileRes.data 
						                console.log('data>>', JSON.parse(data));
						                console.log('data>url>', JSON.parse(data).data);
						                const imageurl  = JSON.parse(data).data
						                console.log('imageurl>>>',imageurl)
						                urllist.push(imageurl)
						                resolve();
						            }
						        });
						    }));
						}
						Promise.all(promises).then(() => {
						    this.urllist = urllist
						    uni.hideLoading()
						    uni.showLoading({
						        title: '等待1秒后回到上页面'
						    })
						    this.goback()
						}).catch((error) => {
						    console.error('Error:', error);
						});

					} catch (e) {
						//TODO handle the exception
						uni.hideLoading({
							title: '上传图片失败...'
						})
					}

				} else {
					uni.showToast({
						title: '请上传报警图片',
						icon:'error'

					})
				}

			}

		}
	}
</script>

<style>
	.button-container {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 0 20rpx;
		margin-bottom: 40px;
		height: 50px;
		background-color: #f5f5f5;
	}

	.button {
		flex: 1;
		margin-right: 10px;
		margin-left: 10px;

		height: 100%;
		text-align: center;
		line-height: 50px;
		font-size: 16px;
		border-radius: 5px;

		color: #fff;


	}
</style>